package com.china08.yxyapi.api.manage.sch.platform.xiaoben.kecheng;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLessonItem;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonItemRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class KeChengItemStatusUpdController extends YxyApiBaseController {

    @Autowired
    YxyLessonsSchoolLessonItemRepository itemRepository;

    @Autowired
    YxyLessonsSchoolLessonRepository lessonRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/sch/xiaoben/kecheng/item/status/upd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String itemId = reqModel.id;
        Integer status = reqModel.status;
        String rejectReason = reqModel.rejectReason;

        YxyLessonsSchoolLessonItem item = itemRepository.findById(itemId);
        if (null != item) {
            item.setStatus(status);
            item.setRejectReason(rejectReason);
            itemRepository.save(item);

            // // 获取其他课时，全部课时通过后，课程功过
            // String lessonId = item.getLessonId();
            //
            // boolean b = true;
            // List<YxyLessonsSchoolLessonItem> items = itemRepository.findByLessonId(lessonId);
            // for (YxyLessonsSchoolLessonItem it : items) {
            // Integer itemStatus = it.getStatus();
            // if (itemStatus != 2) {
            // b = false;
            // break;
            // }
            // }
            //
            // if (b) {
            // YxyLessonsSchoolLesson lesson = lessonRepository.findById(lessonId);
            // lesson.setStatus(2);
            // lesson.setIsVisible(1);
            // lessonRepository.save(lesson);
            // }
        }
    }

    public static class ReqModel extends YxyApiPageReqModel {
        public String id;
        public Integer status;
        public String rejectReason;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(id), "未获取课时");
            if (status == 3) {
                Assert.isTrue(StringUtils.isNotBlank(rejectReason), "申请拒绝原因为空");
            }
        }
    }
}
